<template>
  <div>
    <div :class="['vm-examine-details-f',btnType != 0 ? 'n' : '']" v-if="show == 'examinePage'">
      <mt-button plain @click="checkProcess">审批详情</mt-button>
      <mt-button type="primary" v-if="btnType == 0" @click="isExamine">审批</mt-button>
    </div>
    <!-- 审批弹框 -->
    <mt-popup
      v-model="examine"
      position="bottom"
      class="vm-examine-warp">
      <div class="examine-warp">
        <div class="examine-t">
          <i class="vm-icon examine"></i>
          <h5>审核</h5>
        </div>
        <div class="examine-b">
          <div class="examine-b-btn">
            <mt-button :class="[{'active': isActive1}]" @click="isActive1 = true;isActive2 = false;">通过
            </mt-button>
            <mt-button :class="[{'active': isActive2}]" size="small" @click="isActive2 = true;isActive1 = false;">不通过
            </mt-button>
          </div>
          <div class="examine-b-con">
            <div class="examine-b-con-reason">
              <i class="vm-icon reason"></i>
              <h5>原因</h5>
            </div>
            <input type="text" v-model="reasonData" class="examine-b-con-reason" placeholder="请输入内容">
          </div>

        </div>
        <div class="examine-f">
          <mt-button type="danger" @click="submitExamine">提交</mt-button>
        </div>
      </div>
    </mt-popup>

    <vm-common-examine-process
      ref="examine_process"></vm-common-examine-process>
  </div>

</template>

<script>
  import {checkuserstat, doworkflow, getworkflowrecord, updatestate} from '@/api/examine.js'

  export default {
    data() {
      return {
        examine: false, // 审批按钮弹框
        isActive1: false, // 审批通过
        isActive2: false, // 审批不通过
        reasonData: '', // 审批描述
      }
    },
    props: ["show", "btnType", "objType", "detailsId", "item"],
    created () {},
    methods: {
      // 是否有审批权限
      isExamine() {
        checkuserstat(this.objType, this.detailsId).then(res => {
          const isStatus = res.data;
          if (isStatus == 1) {
            this.examine = !this.examine;
          } else {
            this.$toast(isStatus);
          }
        });
      },
      // 点击提交审批
      async submitExamine() {
        let objType = this.objType; // 项目类型
        let id = this.detailsId; // 单据id
        let opinion;
        if (!this.isActive1 && !this.isActive2) {
          this.$toast('请选择是否通过审批！')
          return;
        } else {
          opinion = this.isActive1 ? 1 : 2;
        }
        let description = this.reasonData;
        let res = await doworkflow(objType, id, opinion, description).then(res => res);

        if (res.data.data == 1 && res.data.status == 200) {
          await updatestate(objType, id).then(res => {
            if (res.data.data == 1 && res.data.status == 200) {
              this.$toast(res.data.message);
              setTimeout(() => {
                this.$router.push({
                  path: '/examine'
                });
              }, 2000);
            }
          })
        } else if (res.data.data == 2 && res.data.status == 200) {
          this.$toast('操作成功！');
          setTimeout(() => {
            this.$router.push({
              path: '/examine'
            });
          }, 2000);
        } else {
          this.$toast('操作失败！');
          setTimeout(() => {
            this.$router.push({
              path: '/examine'
            });
          }, 2000);
        }
      },
      // 查看审批流程
      checkProcess() {
        this.$refs.examine_process.init(this.item, null, {
          oId: this.detailsId,
          objType: this.objType
        });
      }
    }
  }
</script>

<style scoped lang="scss">
  .vm-examine-details-f {
    display: flex;
    height: 150px;
    width: 100%;
    padding: 0 30px;
    align-items: center;
    button {
      border-radius: 10px; /* no */
      height: 85px;
      flex: 1;
      font-size: 32px;
    }
    button:nth-child(1) {
      background-color: #ffffff;
      border: 2px solid #46a8fc; /* no */
      margin-right: 30px;
      color: #83c2ff;
    }
    button:nth-child(2) {
      background-color: #46a8fc;
      border: 2px solid #46a8fc; /* no */
      margin-right: 30px;
      color: #fff;
    }
    &.n {
      button {
        background-color: #46a8fc;
        border: 2px solid #46a8fc; /* no */
        margin-right: 30px;
        color: #fff;
      }
    }
  }

  /* 审批弹框 */
  .vm-examine-warp {
    width: 100%;
    .examine-warp {
      width: 100%;
      height: 100%;
      background-color: #fff;
      padding: 0 28px 0px 25px;
      .examine-t {
        height: 87px;
        display: flex;
        align-items: center;
        border-bottom: 2px solid #f4f4f4; /* no */
        margin-bottom: 40px;
        i.vm-icon.examine {
          display: inline-block;
          width: 26px;
          height: 26px;
          padding-right: 50px;
          background-position: -123px -55px;
        }
        h5 {
          font-size: 28px;
          color: #333;
          font-weight: bold;
        }
      }
      .examine-b {
        margin-bottom: 30px;
        .examine-b-con {
          display: flex;
          flex-direction: column;
          .examine-b-con-reason {
            display: flex;
            i.vm-icon.reason {
              display: inline-block;
              width: 26px;
              height: 26px;
              background-position: -123px -83px;
              padding-right: 50px;
            }
            h5 {
              display: flex;
              height: 55px;
              font-size: 28px;
              color: #333;
              font-weight: bold;
              border-bottom: 2px solid #f4f4f4; /* no */
              margin-bottom: 40px;
            }
          }
          input.examine-b-con-reason {
            min-height: 200px;
            width: 100%;
            border-radius: 15px; /* no */
            font-size: 24px;
          }
        }
      }
      .examine-f {
        margin-bottom: 30px;
        button {
          border-radius: 10px; /* no */
          width: 100%;
          height: 90px;
          background-color: #f34c4f;
        }
      }
    }
  }
</style>
